package com.netsdk.common;

import cn.hutool.extra.spring.SpringUtil;
import com.netsdk.dao.PassengerRecognitionDao;
import com.netsdk.entity.PassengerRecognitionDO;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

@Slf4j
public class AnalyzerDataCallBack implements NetSDKLib.fAnalyzerDataCallBack {
        private AnalyzerDataCallBack() {}

        private PassengerRecognitionDao recognitionDao;

        private static AnalyzerDataCallBack instance = null;

        public static AnalyzerDataCallBack getInstance() {
            return instance == null ? instance = new AnalyzerDataCallBack() : instance;
        }

        // 保存识别结果
        private void saveRecognition(String netTime, int channel, NetSDKLib.FACERECOGNITION_PERSON_INFO personInfo) {
            if (recognitionDao == null) {
                recognitionDao = SpringUtil.getBean(PassengerRecognitionDao.class);
            }
            PassengerRecognitionDO recognitionDO = new PassengerRecognitionDO();
            recognitionDO.setNetTime(netTime);
            recognitionDO.setChannel(String.valueOf(channel));
            recognitionDO.setSzId(new String(personInfo.szID).trim());
            recognitionDO.setSzUid(new String(personInfo.szUID).trim());
            recognitionDO.setPassengerUuid(new String(personInfo.szPersonNameEx).trim());
            recognitionDao.insert(recognitionDO);
        }

        @Override
        public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException {
            // 279 = dwAlarmType 人脸识别
            log.info("收到事件, dwAlarmType = {}", dwAlarmType);
            if (dwAlarmType == NetSDKLib.EVENT_IVS_FACERECOGNITION) {
                NetSDKLib.DEV_EVENT_FACERECOGNITION_INFO recognitionInfo = new NetSDKLib.DEV_EVENT_FACERECOGNITION_INFO();
                ToolKits.GetPointerData(pAlarmInfo, recognitionInfo);

                NetSDKLib.NET_FACE_DATA faceData = recognitionInfo.stuFaceData;
                String netTime = recognitionInfo.UTC.toStringTime();
                log.info("事件发生时间: {}", netTime);

                // 保存全景图
//                saveImage(pBuffer, recognitionInfo);

                log.info("nCandidateNum: {}, nRetCandidatesExNum: {}", recognitionInfo.nCandidateNum, recognitionInfo.nRetCandidatesExNum);
                NetSDKLib.CANDIDATE_INFO[] stuCandidates = recognitionInfo.stuCandidates;
                for (int i = 0; i < recognitionInfo.nCandidateNum; i++) {
                    NetSDKLib.FACERECOGNITION_PERSON_INFO personInfo = stuCandidates[i].stPersonInfo;
                    // 保存识别结果
                    saveRecognition(netTime, recognitionInfo.nChannelID, personInfo);
                }

                log.info("人脸数据: 性别 = {}, 年龄 = {}, 质量 = {}", faceData.emSex, faceData.nAge, faceData.nFaceQuality);
                log.info("nChannelID: {}, szName: {}", recognitionInfo.nChannelID, new String(recognitionInfo.szName));
                log.info("检测到的物体: {}, 置信度(0~255): {}\n", new String(recognitionInfo.stuObject.szObjectType), recognitionInfo.stuObject.nConfidence);
            }
            return 0;
        }

        /*private static class AnalyzerDataCBHolder {
            private static final AnalyzerDataCB instance = new AnalyzerDataCB();
        }

        public static AnalyzerDataCB getInstance() {
            return AnalyzerDataCBHolder.instance;
        }*/
    }